package com.vercon.moensmart.utils;

import android.text.TextUtils;

import com.vercon.common.utils.LogUtils;
import com.vercon.serialportlibrary.Device;
import com.vercon.serialportlibrary.SerialPortFinder;
import com.vercon.serialportlibrary.SerialPortManager;
import com.vercon.serialportlibrary.listener.OnOpenSerialPortListener;
import com.vercon.serialportlibrary.listener.OnSerialPortDataListener;

import java.util.Arrays;
import java.util.List;

public class SerialPortControl implements OnOpenSerialPortListener {
    private final String TAG = "SerialPortUtils";
    private String path = "/dev/ttyS4";  // 可根据设备不同修改
    private int baudrate = 115200; //波率

    private SerialPortManager mSerialPortManager;

    /**
     * 获取设备列表
     */
    public List<Device> getDevice() {
        SerialPortFinder serialPortFinder = new SerialPortFinder();
        List<Device> devices = serialPortFinder.getDevices();
        return devices;
    }

    /**
     * 打开串口
     *
     * @return serialPort串口对象
     */
    public void openSerialPort(String device, int baudrate) {
        // 打开串口
        mSerialPortManager = new SerialPortManager();

        boolean openSerialPort = mSerialPortManager.setOnOpenSerialPortListener(this)
                .setOnSerialPortDataListener(new OnSerialPortDataListener() {
                    @Override
                    public void onDataReceived(byte[] bytes) {
                        //接收
                        LogUtils.i(TAG, "onDataReceived [ byte[] ]: " + Arrays.toString(bytes));
                        LogUtils.i(TAG, "onDataReceived [ String ]: " + new String(bytes));
                        if (onDataReceiveListener != null) {
                            onDataReceiveListener.onDataReceive(bytes, bytes.length);
                        }
                    }

                    @Override
                    public void onDataSent(byte[] bytes) {
                        //发送回调，不能说明发送成功
                        LogUtils.i(TAG, "onDataSent [ byte[] ]: " + Arrays.toString(bytes));
                        LogUtils.i(TAG, "onDataSent [ String ]: " + new String(bytes));
                        if (onDataReceiveListener != null)
                            onDataReceiveListener.onDataSent(bytes);
                    }
                })
                .openSerialPort(device, baudrate);
        LogUtils.i(TAG, "onCreate: openSerialPort = " + openSerialPort);
    }

    /**
     * 发送数据
     */
    public void onSend(String sendContent) {
        if (TextUtils.isEmpty(sendContent)) {
            LogUtils.i(TAG, "onSend: 发送内容为 null");
            return;
        }

        String trim = sendContent.trim().replace(" ", "");
        if (TextUtils.isEmpty(trim)) {
            LogUtils.i(TAG, "onSend: 发送内容为 null");
            return;
        }

        byte[] sendContentBytes = ByteUtil.hexStringToBytes(trim);

        boolean sendBytes = mSerialPortManager.sendBytes(sendContentBytes);
        LogUtils.i(TAG, "onSend: sendBytes = " + sendBytes);
    }

    @Override
    public void onSuccess(String file) {
        //发送成功回调
        // LogUtils.e(TAG, "发送成功回调 " + file.getName());
        if (onDataReceiveListener != null)
            onDataReceiveListener.onSuccess(file);
    }

    @Override
    public void onFail(String file, Status status) {
        //发送失败回调
        //  LogUtils.e(TAG, "发送失败回调 " + file.getName());
        if (onDataReceiveListener != null)
            onDataReceiveListener.onFail(file, status);
    }

    /**
     * 关闭串口
     */
    public void closeSerialPort() {
        if (null != mSerialPortManager) {
            mSerialPortManager.closeSerialPort();
            mSerialPortManager = null;
        }
        LogUtils.d(TAG, "closeSerialPort: 关闭串口成功");
    }

    //监听接收数据
    private OnDataReceiveListener onDataReceiveListener = null;

    public interface OnDataReceiveListener {
        void onDataReceive(byte[] bytes, int size);

        void onDataSent(byte[] bytes);

        void onSuccess(String file);

        void onFail(String file, Status status);

    }

    public void setOnDataReceiveListener(OnDataReceiveListener dataReceiveListener) {
        onDataReceiveListener = dataReceiveListener;
    }

}